﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Statistic
{
    public class Worker
    {
        private IList<IReader> readers = new List<IReader>();
        private IList<Advertisements> advs = new List<Advertisements>();
        private List<Advertisements> allAdvs = new List<Advertisements>();
        private IList<string> keywords_ = new List<string>();
        string date_;
        string htmlString ="";

        public Worker(IList<string> keywords, string date)
        {
            keywords_ = keywords;
            date_ = date;
        }

        public void AddReader(IReader reader)
        {
            readers.Add(reader);
        }

        public void DoIt()
        {                      
            //addReader();
            for (int i = 0; i < readers.Count; i++)
            {
                advs = readers[i].DoTheWork();
                allAdvs.AddRange(advs);
            }
            string fullString = MakeHtmlString();
            Parser skyParser = new Parser();
            string newString = skyParser.HtmlToXmlString(fullString);
            XmlWriter xml = new XmlWriter();
            xml.WriteToXml(newString);
            StatsAnalyzer statsAnalyzer = new StatsAnalyzer(keywords_, date_);
            statsAnalyzer.ReadXml("products.xml");
        }

        public string MakeHtmlString()
        {
            for(int i=0;i<allAdvs.Count;i++)
            {
                int end = allAdvs.Count - 1;
                if (i == end)
                {
                    htmlString += allAdvs[i].Content + "§";
                    htmlString += allAdvs[i].Date;
                }
                else 
                {
                    htmlString += allAdvs[i].Content + "§";
                    htmlString += allAdvs[i].Date + "§";
                }
            }
            return htmlString;
        }
    }
}
